package com.topboya.linker.ui.activity;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.topboya.linker.AppConst;
import com.topboya.linker.R;
import com.topboya.linker.model.response.CompanyUserListReponse;
import com.topboya.linker.model.response.FollowUserByOfficeResponse;
import com.topboya.linker.model.response.FollowUserLateResponse;
import com.topboya.linker.ui.adapter.FlowdepTagAdapter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.FollowUserPrensenter;
import com.topboya.linker.ui.view.FollowUserView;
import com.topboya.linker.utils.LogUtils;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class FollowUserAcitivity extends BaseActivity<FollowUserView,FollowUserPrensenter> implements FollowUserView {

    private static final String TAG = "FollowUserAcitivity:";
    private TextView toolbar_title;
    private TextView tv_toolbar_right;
    private RelativeLayout rl_samedeptorlate;
    private RelativeLayout rl_org;
    private TagFlowLayout id_flowlayout;
    private RecyclerView recycle_org;
    private  FlowdepTagAdapter flowdepTagAdapter;
    private List<String> lateorsanmelist;
    private Button btn_samedeptorlate;
    private Button btn_org;
    private Button btn_select;
    private  ArrayList<FollowUserLateResponse.LateUser> selectlateUsersList;
    private Set<Integer> set;
//    同组和部门切换
    private boolean islate ;


    @Override
    protected int getLayoutId() {

        return R.layout.activity_follow_user;
    }

    @Override
    protected void initView() {

        toolbar_title =findViewById(R.id.toolbar_title);
        tv_toolbar_right =findViewById(R.id.tv_toolbar_right);
        tv_toolbar_right.setVisibility(View.GONE);
        rl_samedeptorlate =findViewById(R.id.rl_samedeptorlate);
        rl_org =findViewById(R.id.rl_org);
        id_flowlayout =findViewById(R.id.id_flowlayout);
        recycle_org =findViewById(R.id.recycle_org);
        btn_samedeptorlate =findViewById(R.id.btn_samedeptorlate);
        btn_org =findViewById(R.id.btn_org);
        btn_select =findViewById(R.id.btn_select);
        recycle_org.setVisibility(View.GONE);
        id_flowlayout.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initData() {
        islate =true;
        toolbar_title.setText("随访人");
        mPresenter.getFollowUser("1");
        lateorsanmelist = new ArrayList<>();
        selectlateUsersList = new ArrayList<>();
        flowdepTagAdapter = new FlowdepTagAdapter(this,id_flowlayout,lateorsanmelist);
        id_flowlayout.setAdapter(flowdepTagAdapter);
    }

    @Override
    protected void initListener() {

            rl_samedeptorlate.setOnClickListener(v -> {
            id_flowlayout.setVisibility(View.VISIBLE);
            recycle_org.setVisibility(View.GONE);
            if (!islate){

                btn_samedeptorlate.setText("同部门");
                 mPresenter.getFollowUser("1");
                islate=true;
            }else {

                btn_samedeptorlate.setText("最近");
                LogUtils.v(TAG + "AppConst.OFFICEID:"+AppConst.OFFICEID);
                mPresenter.getFollowUserByOffice(AppConst.OFFICEID);
                islate=false;
            }

        });

        rl_org.setOnClickListener(v -> {

            recycle_org.setVisibility(View.VISIBLE);
            id_flowlayout.setVisibility(View.GONE);




        });

        id_flowlayout.setOnSelectListener(selectPosSet -> {

            LogUtils.v("choose:" + selectPosSet.toString());
            int size =selectPosSet.size();
            btn_select.setText("选择（"+size+"）");
            set =selectPosSet;
            });


        btn_select.setOnClickListener(v -> {

            LogUtils.v(TAG +"set.toString():"+set.toString());
            selectlateUsersList.clear();
            for (Integer select : set) {
                LogUtils.v(TAG +"set.toString():"+select.toString());

                selectlateUsersList.add(lateUserlist.get(select));
            }
            Intent intent = new Intent(this,CreateTaskActivity1.class);
            intent.putExtra("selectlateUsersList", selectlateUsersList);
            setResult(1003, intent);
            finish();
        });

    }

    @Override
    protected FollowUserPrensenter createPresenter() {


        return new FollowUserPrensenter(this);
    }

    private List<FollowUserLateResponse.LateUser> lateUserlist;
    @Override
    public void getFollowUser(List<FollowUserLateResponse.LateUser> lateUserlist) {
        this.lateUserlist = lateUserlist;

        lateorsanmelist.clear();
        for (int i=0;i < lateUserlist.size() ;i++){

            lateorsanmelist.add(lateUserlist.get(i).getName());
        }
        flowdepTagAdapter.notifyDataChanged();

    }
    private List<FollowUserByOfficeResponse.SameDepartList> sameDepartLists;
    @Override
    public void getFollowUserByOffice(List<FollowUserByOfficeResponse.SameDepartList> sameDepartLists) {

        this.sameDepartLists = sameDepartLists;

        lateorsanmelist.clear();
        for (int i=0;i < sameDepartLists.size() ;i++){

            lateorsanmelist.add(sameDepartLists.get(i).getName());
        }
        flowdepTagAdapter.notifyDataChanged();

    }

    @Override
    public void getCompanyUserList(List<CompanyUserListReponse.Data> comuserlists) {


    }


}
